package com.pl.lc.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.param.UserVipAdd;
import com.pl.lc.common.vo.R;
import com.pl.lc.user.entity.MoneyLog;
import com.pl.lc.user.entity.PayLog;
import com.pl.lc.user.entity.VipOrder;
import com.pl.lc.user.entity.Wallet;
import com.pl.lc.user.mapper.MoneyLogMapper;
import com.pl.lc.user.mapper.PayLogMapper;
import com.pl.lc.user.mapper.WalletMapper;
import com.pl.lc.user.service.PayLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pl.lc.user.service.VipOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 10.支付记录表 服务实现类
 * </p>
 *
 * @author pl
 * @since 2024-06-12
 */
@Service
public class PayLogServiceImpl extends ServiceImpl<PayLogMapper, PayLog> implements PayLogService {
    @Autowired
    private PayLogMapper payLogMapper;
    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private MoneyLogMapper moneyLogMapper;
    @Autowired
    private RedisTemplate  redisTemplate;
    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private VipServiceImpl vipService;

    @Override
    public R callBack(String no) {
        // 1.修改支付订单状态
        QueryWrapper<PayLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", no);
        PayLog payLog = payLogMapper.selectOne(queryWrapper);
        if (payLog == null) {
            return R.fail("充值失败");
        }
        //设置支付订单日志为已支付
        payLog.setStatus(2);
        payLogMapper.updateById(payLog);

        if (no.startsWith("z")) {//支付宝充值账户余额
            //2.钱包余额增加
            QueryWrapper<Wallet> walletQueryWrapper = new QueryWrapper<>();

            walletQueryWrapper.eq("user_id", payLog.getUserId());
            Wallet wallet = walletMapper.selectOne(walletQueryWrapper);
            if (wallet==null){
                return R.fail("充值失败");
            }else{
                wallet.setMoney(wallet.getMoney()+payLog.getMoney()/100);
                wallet.setTotalMoney(wallet.getTotalMoney()+payLog.getMoney()/100);
                int i = walletMapper.updateById(wallet);
                if (i<=0){
                    return R.fail("充值失败");
                }
            }

            //  3. 写入钱包增加日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setUserId(payLog.getUserId());
            moneyLog.setMoney(payLog.getMoney()/100);
            //增加余额
            moneyLog.setType(1);
            moneyLog.setFlag("使用支付宝爱贝充值");
            moneyLog.setInfo("支付宝充值");
            moneyLog.setCtime(new Date());
            moneyLogMapper.insert(moneyLog);

            return R.ok("充值成功");
        } else if (no.startsWith("v")) {//使用支付宝购买vip
            // 3.修改会员订单状态
            VipOrder vipOrder = (VipOrder) redisTemplate.opsForValue().get("vipOrder");
            UserVipAdd userVipAdd = (UserVipAdd) redisTemplate.opsForValue().get("userVipAdd");
            vipOrder.setStatus(2);
            vipOrderService.updateById(vipOrder);
            //4.创建，购买 或者 续费 vip会员
            boolean flag = vipService.createVip(userVipAdd);
            //5.返回
            if (flag) {
                return R.ok("redirect:http://localhost:5173/vip");
            }
            return R.fail("购买失败");
        }

        return R.fail("充值失败");
    }
}
